# OpenOCD flasher script for RTL8710
#
# Author : Snow Yang
# Date   : 2017-12-20
#

set updata_ram     0x10002000
set buffer_size    0x1000

set update_dest_addr       [expr $updata_ram + 0x00]
set update_file_size       [expr $updata_ram + 0x04]
set update_stream_size     [expr $updata_ram + 0x08]
set update_status          [expr $updata_ram + 0x0C]
set update_command         [expr $updata_ram + 0x10]
set update_data_buffer     [expr $updata_ram + 0x14]

set UPDATE_STATE_READY                     1
set UPDATE_STATE_SUCCESS                   2
set UPDATE_STATE_ERROR_FILE_TO_LARGE        3
set UPDATE_STATE_ERROR_WRTIE_EFUSE_FAILED   4
set UPDATE_STATE_ERROR_INVALID_COMMAND      5

set UPDATE_COMMAND_WRITE_FLASH  1
set UPDATE_COMMAND_WRITE_EFUSE  2
set UPDATE_COMMAND_READ_EFUSE   3
set UPDATE_COMMAND_ENABLE_RDP   4
set UPDATE_COMMAND_ERASE_CHIP   5

proc memread32 {address} {
    mem2array memar 32 $address 1
    return $memar(0)
}

proc load_image_bin {fname foffset address length } {
    # Load data from fname filename at foffset offset to
    # target at address. Load at most length bytes.
    puts "loadimage address $address foffset $foffset $length"
    load_image $fname [expr $address - $foffset] bin $address $length
}

proc wait_result { } {
    set loops  0
    set resultval $::UPDATE_STATE_READY
    resume
    while { ($resultval == $::UPDATE_STATE_READY) && ( $loops < 1000 ) } {
        sleep 10
        set resultval [memread32 $::update_status]
        incr loops
    }
    halt

    if { $resultval != $::UPDATE_STATE_SUCCESS } {
        reg
        error "program_sflash error, Result: $resultval"
        exit -1;
    }
}

proc flasher_init { } {
    global rtl8710_flasher_code

    reset halt
    array2mem rtl8710_flasher_code 32 0x10004000 [array size rtl8710_flasher_code]
    reg sp 0x1003EFFC
    reg pc 0x10004000
}

proc erase_chip { } {
    flasher_init

    puts "Erase chip"
    mww $::update_command   $::UPDATE_COMMAND_ERASE_CHIP
    mww $::update_status $::UPDATE_STATE_READY

    wait_result
}

proc program_flash { file_name dest_addr } {
    
    flasher_init

    set pos 0
    set bin_file_size [file size $file_name]

    puts "Write $file_name to flash, total size is $bin_file_size"
    mww $::update_dest_addr $dest_addr
    mww $::update_file_size $bin_file_size
    mww $::update_command   $::UPDATE_COMMAND_WRITE_FLASH

    while { $pos < $bin_file_size } {

        if { ($bin_file_size - $pos) <  $::buffer_size } {
            set writesize [expr ($bin_file_size - $pos)]
        } else {
            set writesize $::buffer_size
        }

        puts "Writing $writesize bytes at [expr $dest_addr + $pos]"
        # Load the binary data into the RAM
        load_image_bin $file_name $pos $::update_data_buffer $writesize
        mww $::update_stream_size $writesize
        mww $::update_status $::UPDATE_STATE_READY

        wait_result

        set pos [expr $pos + $writesize]
    }
}

proc program_efuse { file_name dest_addr } {
    flasher_init

    set bin_file_size [file size $file_name]

    puts "Write $file_name to efuse, total size is $bin_file_size"
    mww $::update_dest_addr $dest_addr
    mww $::update_file_size $bin_file_size
    mww $::update_command   $::UPDATE_COMMAND_WRITE_EFUSE

    # Load the binary data into the RAM
    load_image_bin $file_name 0 $::update_data_buffer $bin_file_size
    mww $::update_stream_size $bin_file_size
    mww $::update_status $::UPDATE_STATE_READY

    wait_result
}

proc dump_efuse { } {    
    flasher_init

    puts "Dumping efuse map to \"efuse.dump.bin\""
    mww $::update_command   $::UPDATE_COMMAND_READ_EFUSE
    mww $::update_status $::UPDATE_STATE_READY

    wait_result

    dump_image efuse.dump.bin $::update_data_buffer 512
}

proc enable_rdp { } {    
    flasher_init

    puts "Enable RDP"
    mww $::update_command   $::UPDATE_COMMAND_ENABLE_RDP
    mww $::update_status $::UPDATE_STATE_READY

    wait_result
}

array set rtl8710_flasher_code {
	0 0x4C3CB5F8 1 0x2B016923 2 0x6823D11C 3 0x44136862 4 0x1F00F5B3 5 0x2303D902
	6 0xE7FE60E3 7 0x47984B36 8 0x4F362500 9 0x68E34E36 10 0xD1FC2B01 11 0x68A16820
	12 0x31034428 13 0x0889463A 14 0x68A347B0 15 0x2302441D 16 0xE7F060E3 17 0x2B026923
	18 0x6823D128 19 0x44136862 20 0x7F00F5B3 21 0x2303D902 22 0xE7FE60E3 23 0x68204D27
	24 0x6861462A 25 0x47984B27 26 0x48284B27 27 0x23004798 28 0x4A266820 29 0x44026861
	30 0xD1034299 31 0x4B1D2202 32 0xE7FE60DA 33 0x5CE85CD4 34 0xD1014284 35 0xE7F33301
	36 0xD0F32B00 37 0x4B172204 38 0xE7FE60DA 39 0x2B036923 40 0x4B19D10F 41 0x47984819
	42 0x4A142300 43 0x5C194817 44 0x330154D1 45 0x7F00F5B3 46 0x2202D1F9 47 0x60DA4B0D
	48 0x6923E7DF 49 0xD1072B04 50 0x47984B11 51 0x48124B11 52 0x23024798 53 0xE7D460E3
	54 0x2B056923 55 0x4B06D106 56 0x4B0E4798 57 0x23024798 58 0xE7CA60E3 59 0x60E32305
	60 0xBF00E7FE 61 0x10002000 62 0x10004BE1 63 0x10002014 64 0x10004C6D 65 0x10004933
	66 0x100041A5 67 0x10004CD8 68 0x00003CAD 69 0x00003CCD 70 0x10004CC8 71 0x10004C61
	72 0x4FF8E92D 73 0x2600468B 74 0x468224FF 75 0x46984615 76 0xF1BB46B1 77 0xDD060FFF
	78 0xF242B280 79 0x428861AE 80 0x702CBF18 81 0x4642D12B 82 0x20002101 83 0xFDA8F000
	84 0x7828F8DF 85 0xF4206878 86 0x60786000 87 0x518BEA4F 88 0x081CF8DF 89 0x000AEA00
	90 0xEA400D89 91 0x60382001 92 0x6A20F644 93 0x2005E003 94 0xFD96F000 95 0x68381C76
	96 0xD4012800 97 0xD3F64556 98 0xBF3C4556 99 0xF04F683C 100 0x702C0901 101 0x21004642
	102 0xF0002000 103 0x4648FD81 104 0x8FF2E8BD 105 0x4FF8E92D 106 0xF8DF4680 107 0xF8D007D0
	108 0x25009000 109 0x7200F44F 110 0x464021FF 111 0xFD78F000 112 0x0B0CF60F 113 0xA7BCF8DF
	114 0xF10D2307 115 0x46290201 116 0xF7FF4648 117 0xF89DFFA5 118 0x1C6D0001 119 0xD10928FF
	120 0x0000F8DA 121 0xD56502C0 122 0xF20FB2A9 123 0xF00070A8 124 0xE05FFCF3 125 0x001FF000
	126 0xF89D280F 127 0xD1170001 128 0x23070946 129 0x0202F10D 130 0x4648B2A9 131 0xFF88F7FF
	132 0x0002F89D 133 0xF0001C6D 134 0x280F000F 135 0xF89DD047 136 0x08400002 137 0x0078F000
	138 0xF89D4306 139 0xE0000002 140 0xF0000906 141 0x2E40040F 142 0x00F6DA28 143 0x21012700
	144 0x420C40B9 145 0xF000D11C 146 0xF89DF838 147 0xF8060000 148 0x1C6D0008 149 0xF831F000
	150 0x2E111C6D 151 0xF8DADA0B 152 0x02C00000 153 0xF89DD507 154 0xF8163000 155 0x46312008
	156 0xF0004658 157 0xF89DFCB1 158 0xEB060000 159 0x70480108 160 0x1C7F1CB0 161 0x2F04B286
	162 0xE010DBD9 163 0x07E12000 164 0x2001BF58 165 0xBF5807A1 166 0x07611C40 167 0x1C40BF58
	168 0xBF580721 169 0xB2AD1C40 170 0xEB05B2C0 171 0xB2AD0540 172 0xDB892D7E 173 0xE8BD2001
	174 0x23078FF2 175 0xB2A9AA00 176 0xE72D4648 177 0x16C0F8DF 178 0x2801680A 179 0xF022BF0C
	180 0xF0426080 181 0x60086080 182 0xB5F84770 183 0x20FF4604 184 0x0000F88D 185 0x4616460D
	186 0x4620461F 187 0xFCE8F000 188 0xF7FF2001 189 0xF000FFE7 190 0xF89DF820 191 0x42B00000
	192 0x463BD010 193 0x46294632 194 0xF0004620 195 0xF8DFFCD9 196 0x6800067C 197 0xBF440040
	198 0x60CCF20F 199 0xFC5CF000 200 0xF80BF000 201 0xF7FF2000 202 0xF89DFFCD 203 0x42B00000
	204 0x2000D001 205 0x2001BDF2 206 0x463BBDF2 207 0x4629AA00 208 0xF0004620 209 0xE92DBCB9
	210 0x46054FF0 211 0x062CF8DF 212 0xF8D0B083 213 0x460CA000 214 0x27002600 215 0x000FF004
	216 0x46B04691 217 0xBF08280F 218 0xD0612000 219 0xAA012307 220 0x46504631 221 0xFED4F7FF
	222 0x0004F89D 223 0xD02428FF 224 0x000FF000 225 0xD110280F 226 0xFA03F000 227 0xF7FF4650
	228 0xF89DFEC7 229 0x43C00005 230 0x000FF000 231 0x07C1B188 232 0x1CB6BF44 233 0x1040B2F6
	234 0xF89DE7F8 235 0x43C00004 236 0x000FF010 237 0x07C1E004 238 0x1CB6BF44 239 0x1040B2F6
	240 0x1C76D1F9 241 0x2E7EB2F6 242 0xF004DBD0 243 0x07E0040F 244 0x2702BF58 245 0xBF5C07A0
	246 0xB2C71CB8 247 0xBF5C0760 248 0xB2C71CB8 249 0xBF5C0720 250 0xB2C71CB8 251 0x1C8019B8
	252 0xDA57287E 253 0xF0400168 254 0xF8DF020F 255 0x23078594 256 0x4631B2D2 257 0xF7FF4650
	258 0xB2C0FF68 259 0xD10B2801 260 0x1C760068 261 0x00F0F000 262 0xEA44B2F6 263 0xF0000200
	264 0xB2C0F84A 265 0xD0042801 266 0xF8C82001 267 0x20000000 268 0x1C76E039 269 0x2500B2F6
	270 0x7548F8DF 271 0xF105FA00 272 0xD129420C 273 0x02C06838 274 0x9500D505 275 0xF8194632
	276 0xF0003015 277 0xF819F82A 278 0xF0002015 279 0xB2C0F82C 280 0xD1132801 281 0x1C766838
	282 0xEB09B2F6 283 0x02C00B45 284 0x9500D505 285 0xF89B4632 286 0xF0003001 287 0xF89BF816
	288 0xF0002001 289 0xB2C0F818 290 0xD0032801 291 0xF8C82000 292 0xE0080000 293 0xB2F61C76
	294 0xB2CD1C69 295 0xDBCD2D04 296 0x0801F04F 297 0xE8BD4640 298 0xF20F8FFE 299 0xF20F5158
	300 0xF000506C 301 0x2307BB91 302 0x46504631 303 0xE92DE70D 304 0x460C4FF8 305 0xF1004692
	306 0x2702062C 307 0x5884F20F 308 0x94B8F8DF 309 0x54B8F8DF 310 0x60282000 311 0x465243E1
	312 0xB2F0B2C9 313 0xFF2FF7FF 314 0xF1BB4683 315 0xD00E0F01 316 0xB1086828 317 0xE00A2000
	318 0x0000F8D9 319 0x00401E7F 320 0x4640BF44 321 0xFB68F000 322 0xD1E52F00 323 0xE8BD4658
	324 0xB5388FF2 325 0xF1A12401 326 0x2D200580 327 0xF7FFD202 328 0x4604FEDC 329 0xBD324620
	330 0x4604B5F8 331 0x044CF8DF 332 0x26006805 333 0x0780F106 334 0xAA002307 335 0x4628B2B9
	336 0xFBAAF000 337 0x0000F89D 338 0xD10C28FF 339 0x23075D32 340 0x4628B2B9 341 0xFFDDF7FF
	342 0xD1032801 343 0xB2F61C76 344 0xDBE72E20 345 0x2000BDF2 346 0xF8DFBDF2 347 0x68000410
	348 0x22FE2307 349 0xE6B021D3 350 0x4FF7E92D 351 0x26004604 352 0x03F8F8DF 353 0x6000F88D
	354 0xF8D04688 355 0x4642B000 356 0xF0022700 357 0x280F000F 358 0x2000BF08 359 0x80EEF000
	360 0x53E0F8DF 361 0x02C06828 362 0x4621D504 363 0x40C8F20F 364 0xFB12F000 365 0x682846B1
	366 0xD50702C0 367 0xF8199802 368 0xF20F2000 369 0x464940EC 370 0xFB06F000 371 0x0901F109
	372 0x0F08F1B9 373 0x2307DBEF 374 0x4631AA01 375 0xF7FF4658 376 0xF89DFD9F 377 0x28FF0004
	378 0xF000D024 379 0x280F000F 380 0xF000D110 381 0x4658F8CE 382 0xFD92F7FF 383 0x0005F89D
	384 0xF00043C0 385 0xB188000F 386 0xBF4407C1 387 0xB2F61CB6 388 0xE7F81040 389 0x0004F89D
	390 0xF01043C0 391 0xE004000F 392 0xBF4407C1 393 0xB2F61CB6 394 0xD1F91040 395 0xB2F61C76
	396 0xDBD02E7E 397 0x080FF008 398 0x70C8EA5F 399 0x2702BF58 400 0x7088EA5F 401 0x1CB8BF5C
	402 0xEA5FB2C7 403 0xBF5C7048 404 0xB2C71CB8 405 0x7008EA5F 406 0x1CB8BF5C 407 0x6828B2C7
	408 0x39FCF20F 409 0xD50602C0 410 0x463A4643 411 0xF20F4649 412 0xF0004068 413 0x19B8FAB1
	414 0x287E1C80 415 0x6828DA7C 416 0x4780F20F 417 0xDB5E2C0F 418 0xD50702C0 419 0xF0400160
	420 0x4632030F 421 0x46384649 422 0xFA9EF000 423 0xF0400160 424 0xF000020F 425 0xB2C0F87D
	426 0xD1582801 427 0x1C766828 428 0x02C0B2F6 429 0x0060D502 430 0xF862F000 431 0xF0000060
	432 0xEA4800F0 433 0x23070200 434 0xF86CF000 435 0x2801B2C0 436 0x1C76D145 437 0x2400B2F6
	438 0x3A40F20F 439 0xFA002001 440 0xEA18F104 441 0xD1410F01 442 0x02C06828 443 0x9802D508
	444 0x46329400 445 0x3014F810 446 0x46504649 447 0xFA6CF000 448 0xF8109802 449 0x23072014
	450 0xF84CF000 451 0x2801B2C0 452 0x9802D115 453 0x0744EB00 454 0x1C766828 455 0x02C0B2F6
	456 0x9400D506 457 0x787B4632 458 0x46504649 459 0xFA54F000 460 0x2307787A 461 0xF836F000
	462 0x2801B2C0 463 0x4994D014 464 0x60082000 465 0x02C0E01A 466 0x0120D502 467 0xF818F000
	468 0x1204EA48 469 0xF824F000 470 0x2801B2C0 471 0x498CD0B9 472 0x60082001 473 0xE0092000
	474 0xB2F61C76 475 0xB2E41C64 476 0xDBB32C04 477 0x0000F88D 478 0x0000F89D 479 0x8FFEE8BD
	480 0x00F0F000 481 0x0300EA48 482 0x46494632 483 0xF0004638 484 0x1C76BA23 485 0x2307B2F6
	486 0x0205F10D 487 0x47704631 488 0xB2D22307 489 0x46584631 490 0xE92DE597 491 0x46884FF1
	492 0xF5AD4440 493 0x46157D04 494 0x7F00F5B0 495 0xA803D805 496 0xFCF0F7FF 497 0x0900EA5F
	498 0x2000D101 499 0x9884E09F 500 0x0AD0EA4F 501 0xF8A7F000 502 0xF0009884 503 0x24000607
	504 0xD52307F0 505 0x78289A84 506 0x5C51A903 507 0xD0194288 508 0x08712001 509 0x43C04088
	510 0x070FF000 511 0x1810A803 512 0x2C01F810 513 0x1830A801 514 0x2C01F800 515 0x782BA801
	516 0x485C5433 517 0x02C06800 518 0x1E71D504 519 0x20FCF20F 520 0xF9DAF000 521 0x24011C76
	522 0x1CB6E000 523 0xDA452E08 524 0xD04345A0 525 0x10712001 526 0xEA274088 527 0x98840300
	528 0xA9031820 529 0x0E01EB00 530 0xC001F810 531 0x5D62484D 532 0xA9016800 533 0xEBA81871
	534 0xB2DB0B04 535 0x0F01F1BB 536 0x4562D111 537 0x461FD00D 538 0x02C0AB01 539 0xF89E54F2
	540 0x704B3001 541 0x9700D505 542 0xF20F4631 543 0xF00020C0 544 0x1C64F9AB 545 0x4562E01A
	546 0xEB04BF01 547 0xF89C0C05 548 0xF89EC001 549 0x45F4E001 550 0x461FD00D 551 0x54F2AB01
	552 0x02C01963 553 0x704B785B 554 0x9700D505 555 0xF20F4631 556 0xF00020AC 557 0x1CA4F991
	558 0xD1B64544 559 0xD02A2F0F 560 0x68004830 561 0xD50F02C0 562 0xFA8AFA5F 563 0xF20F4651
	564 0xF00020AC 565 0x482BF981 566 0x02C06800 567 0x4639D504 568 0x20B4F20F 569 0xF978F000
	570 0x4639AA01 571 0xF08AFA5F 572 0xFE42F7FF 573 0x0900EA5F 574 0x4824D10D 575 0x00406800
	576 0x4621D504 577 0x20ACF20F 578 0xF966F000 579 0xF50D4648 580 0xE8BD7D05 581 0x45448FF0
	582 0xF10AD0F8 583 0x26000A01 584 0xF801F000 585 0x270FE784 586 0x21FF2208 587 0xF000A801
	588 0xE92DB9BF 589 0x46814FF8 590 0x4693468A 591 0xF20F2702 592 0x4C12288C 593 0x20004D12
	594 0x465A6028 595 0x46484651 596 0xFF2BF7FF 597 0x2E014606 598 0x6828D00D 599 0x2000B108
	600 0x6820E009 601 0x00401E7F 602 0x4640BF44 603 0xF934F000 604 0xD1E82F00 605 0xE8BD4630
	606 0x00008FF2 607 0x400000E8 608 0x7FFC0000 609 0x10000314 610 0x400000EC 611 0x10000318
	612 0x10004ED8 613 0x46455B0D 614 0x20455355 615 0x5D666E49 616 0x55464520 617 0x4C5F4553
	618 0x6369676F 619 0x614D6C61 620 0x65525F70 621 0x203A6461 622 0x61746164 623 0x646E6520
	624 0x20746120 625 0x72646461 626 0x3D737365 627 0x000A7825 628 0x46455B0D 629 0x20455355
	630 0x5D666E49 631 0x20782520 632 0x25207825 633 0x000A2078 634 0x5546450D 635 0x57204553
	636 0x45544952 637 0x43455320 638 0x20444E4F 639 0x454D4954 640 0x00000A20 641 0x53554645
	642 0x53555F45 643 0x575F5245 644 0x65746972 645 0x5543415F 646 0x00000054 647 0x46455B0D
	648 0x20455355 649 0x5D666E49 650 0x20732520 651 0x73756665 652 0x64615F65 653 0x253A7264
	654 0x6F632078 655 0x6E65746E 656 0x78253A74 657 0x726F7720 658 0x64695F64 659 0x78253A78
	660 0x0000000A 661 0x50544D0D 662 0x5254525F 663 0x00000A59 664 0x53554645 665 0x47505F45
	666 0x6361505F 667 0x5F74656B 668 0x54554341 669 0x00000000 670 0x46455B0D 671 0x20455355
	672 0x5D666E49 673 0x55464520 674 0x505F4553 675 0x61505F47 676 0x74656B63 677 0x66666F20
	678 0x3A746573 679 0x57207825 680 0x4564726F 681 0x6C62616E 682 0x78253A65 683 0x00000A20
	684 0x46455B0D 685 0x20455355 686 0x5D666E49 687 0x55464520 688 0x505F4553 689 0x61505F47
	690 0x74656B63 691 0x78255B20 692 0x7825205D 693 0x00000A20 694 0x46455B0D 695 0x20455355
	696 0x5D666E49 697 0x20732520 698 0x3A6E654C 699 0x20782520 700 0x64726F57 701 0x62616E45
	702 0x203A656C 703 0x0A207825 704 0x00000000 705 0x46455B0D 706 0x20455355 707 0x5D666E49
	708 0x20732520 709 0x25207825 710 0x000A2078 711 0x46455B0D 712 0x20455355 713 0x5D666E49
	714 0x5B203120 715 0x3A5D7825 716 0x20782520 717 0x0A207825 718 0x00000000 719 0x46455B0D
	720 0x20455355 721 0x5D666E49 722 0x5B203220 723 0x3A5D7825 724 0x20782520 725 0x25207825
	726 0x00000A78 727 0x46455B0D 728 0x20455355 729 0x5D666E49 730 0x5B203320 731 0x3A5D7825
	732 0x20782520 733 0x25207825 734 0x00000A78 735 0x46455B0D 736 0x20455355 737 0x5D666E49
	738 0x66666F20 739 0x3D746573 740 0x0A207825 741 0x00000000 742 0x46455B0D 743 0x20455355
	744 0x5D666E49 745 0x726F7720 746 0x6E655F64 747 0x2078253D 748 0x0000000A 749 0x414D4C0D
	750 0x41462050 751 0x69204C49 752 0x203A7864 753 0x78257830 754 0x0000000A 755 0x414D4C0D
	756 0x54525F50 757 0x000A5952 758 0x2000B40F 759 0x4770B004 760 0x4B14B510 761 0x2000B098
	762 0x4C134798 763 0xF44F2200 764 0x20107180 765 0x4B1147A0 766 0x47982000 767 0xF44F2201
	768 0x20107180 769 0x220147A0 770 0x200A2100 771 0x47984B0C 772 0x4B0CA802 773 0x4B0C4798
	774 0x47982000 775 0x2103AA01 776 0x0051F89D 777 0x47984B09 778 0x4B092000 779 0xB0184798
	780 0xBF00BD10 781 0x00005811 782 0x00009B65 783 0x00007D15 784 0x000042FD 785 0x00007C11
	786 0x00007CA1 787 0x00007465 788 0x0000D83D 789 0x4B014601 790 0x47182002 791 0x00007755
	792 0x4B012100 793 0x47184608 794 0x00007755 795 0x460DB5F8 796 0x4F084614 797 0xF1151A86
	798 0x462235FF 799 0x0004EB06 800 0xBDF8D200 801 0x34046813 802 0xD0F43301 803 0x47B82104
	804 0xBF00E7F1 805 0x00007661 806 0xF000F85F 807 0x00003BE1 808 0xF000F85F 809 0x00006C49
	810 0xF000F85F 811 0x00003411 812 0xF000F85F 813 0x00000A7D 814 0xF000F85F 815 0x00006D65
	816 0xF000F85F 817 0x00006E0D 818 0xFE6E1778 819 0x1BD5FC53 820 0x4F9BB189 821 0xCA445644
}
